package com.hefei.customer.info.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.hefei.common.returncode.ReturnCode;
import com.hefei.customer.info.dao.ICustomerDao;
import com.hefei.customer.info.mapper.CustomerMapper;
import com.hefei.customer.info.po.Contact;
import com.hefei.customer.info.po.Customer;
import com.hefei.service.framework.base.request.RequestThreadLocal;
import com.hefei.service.framework.exception.DaoException;
import com.hefei.service.framework.monitor.Warning;



@Repository
public class CustomerDao implements ICustomerDao {
	
	private Logger logger = Logger.getLogger(CustomerDao.class);
	@Autowired
	private CustomerMapper customerMapper;
	@Override
	public void save(Customer customer) throws DaoException {
		if(customer == null){
			throw new DaoException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		long begintimer = System.currentTimeMillis();
		try {
			customerMapper.saveCustomer(customer);
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " save error " , e);
			throw new DaoException(ReturnCode.RETURN_CODE_ERROR);
		}
		Warning.warnFuntionTimer("CustomerInfoDao", "save", null, (System.currentTimeMillis() - begintimer), begintimer);
	}
	@Override
	public void update(Customer customer) throws DaoException {
		if(customer == null){
			throw new DaoException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		long begintimer = System.currentTimeMillis();
		try {
			customerMapper.updateCustomer(customer);
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " update error " , e);
			throw new DaoException(ReturnCode.RETURN_CODE_ERROR);
		}
		Warning.warnFuntionTimer("CustomerInfoDao", "update", null, (System.currentTimeMillis() - begintimer), begintimer);
		
	}
	@Override
	public void delete(Integer id) throws DaoException {
		if(id == null){
			throw new DaoException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		long begintimer = System.currentTimeMillis();
		try {
			customerMapper.deleteCustomer(id);
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " delete error " , e);
			throw new DaoException(ReturnCode.RETURN_CODE_ERROR);
		}
		Warning.warnFuntionTimer("CustomerInfoDao", "delete", null, (System.currentTimeMillis() - begintimer), begintimer);
		
	}
	@Override
	public Customer getCustomerById(Integer id) throws DaoException {
		if(id == null){
			throw new DaoException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		try {
			return customerMapper.getCustomerById(id);
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " delete error " , e);
			throw new DaoException(ReturnCode.RETURN_CODE_ERROR);
		}
		
	}
	@Override
	public Customer getCustoemrByName(String name) throws DaoException {
		if(name == null){
			throw new DaoException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		try {
			return customerMapper.getCustoemrByName(name);
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " delete error " , e);
			throw new DaoException(ReturnCode.RETURN_CODE_ERROR);
		}
	}
	@Override
	public List<Contact> getContactById(Integer customerId) throws DaoException {
		// TODO Auto-generated method stub
		return null;
	}
	@Override
	public Long getDatagridTotal(Customer customer) throws DaoException {
		if(customer == null){
			throw new DaoException(ReturnCode.RETURN_CODE_PARAM_NUL);
		}
		try {
			return customerMapper.getDatagridTotal(customer);
		} catch (Exception e) {
			logger.error(RequestThreadLocal.getLoggerStr() + " delete error " , e);
			throw new DaoException(ReturnCode.RETURN_CODE_ERROR);
		}
	}

}
